app.controller('extractRecordController', function ($scope, $controller, extractRecordService, smsService, uploadService, brandTaxInfoService,taxInfoService) {

    $controller('baseController', {$scope: $scope});

    $scope.entity = {schedulingType: null};

    $scope.searchEntity = {};

    //监控 服务费资金池 调度类型
    $scope.$watch('entity.payerAccountName', function (newValue, oldValue) {
        if ($scope.isNull(newValue)){
            $scope.entity.payerAccount = null;
            $scope.entity.taxInfoId = null;
            $scope.entity.brandId = null;
            return;
        }
        if (3 != $scope.entity.schedulingType) {
            return;
        }

        var brandTaxInfoList = $scope.brandTaxInfoList;

        for (let i = 0; i < brandTaxInfoList.length; i++) {
            brandTaxInfo = brandTaxInfoList[i];
            if (brandTaxInfo.serviceFeeAcctNoName == newValue && $scope.entity.tax_id == brandTaxInfo.taxInfoId) {
                //付款账号
                $scope.entity.payerAccount = brandTaxInfo.serviceFeeAcctNo;
                $scope.entity.taxInfoId = brandTaxInfo.taxInfoId;
                $scope.entity.brandId = brandTaxInfo.brandId;
            }
        }

        //平台服务费
        $scope.entity.serviceFee = 0;

    });

    $scope.prompt = "图片/PDF";

    //上传合同
    $scope.uploadSchedulingVoucher = function () {

        console.log("调度证明上传执行");

        uploadService.uploadSchedulingVoucher().success(function (response) {

            if (response.code == 200) {

                $scope.entity.schedulingVoucher = response.data;

                $scope.prompt = response.msg;

            } else {
                alert(response.msg);
            }
        });

    };

    //提交调度申请
    $scope.submitApplication = function () {

        console.log($scope.isClick);
        if ($scope.isClick) {
            return;
        }
        //标记为点击状态
        $scope.isClick = true;

        if ($scope.isNull($scope.entity.schedulingType)) {

            alert("未选择调度类型");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }


        if ($scope.isNull($scope.entity.payerAccount)) {

            alert("未填写付款账户");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }

        if ($scope.isNull($scope.entity.payerAccountName)) {

            alert("未填写付款账户名");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }

        if ($scope.isNull($scope.entity.payeeAccount)) {

            alert("未填写收款账户");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }

        if ($scope.isNull($scope.entity.payeeAccountName)) {

            alert("未填收款账户名");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }

        if ($scope.entity.schedulingType == 2) {

            if ($scope.isNull($scope.entity.payeeOpeningBank)) {

                alert("未填写收款账户开户行");

                //还原按钮初始状态
                $scope.isClick = false;

                return;
            }

            // if ($scope.isNull($scope.entity.bankFee)) {
            //
            //     alert("未填写银行手续费");
            //
            //     //还原按钮初始状态
            //     $scope.isClick = false;
            //
            //     return;
            // } else if ($scope.entity.bankFee <= 0){
            //
            //     alert("银行手续费金额不正确");
            //
            //     //还原按钮初始状态
            //     $scope.isClick = false;
            //
            //     return;
            // }

        }


        if ($scope.isNull($scope.entity.tranAmount)) {

            alert("未填写出款金额");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        } else if ($scope.entity.tranAmount <= 0) {

            alert("出款金额不正确");

            //还原按钮初始状态
            $scope.isClick = false;

            return;

        }


        if ($scope.isNull($scope.entity.serviceFee)) {

            alert("未填写平台服务费");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        } else if ($scope.entity.serviceFee < 0) {
            alert("平台服务费金额不正确");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }


        if ($scope.entity.payerAccount === $scope.entity.payeeAccount) {

            alert("付款账户与收款账户相同");

            //还原按钮初始状态
            $scope.isClick = false;

            return;

        }

        if ($scope.entity.schedulingType != 3 && $scope.isNull($scope.entity.schedulingVoucher)) {

            alert("未上传调度凭证");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }


        extractRecordService.submitApplication($scope.entity).success(function (response) {

            if (response.code == 200) {
                alert(response.msg);

                $scope.entity = {};
            } else {
                alert(response.msg);
            }

            //还原按钮初始状态
            $scope.isClick = false;

        }).error(function () {
            //还原按钮初始状态
            $scope.isClick = false;
        });

    };


    //获取调度记录
    $scope.findPage = function (pageNum, pageSize) {

        if ($scope.isNull(pageNum))
            pageNum = 1;

        if ($scope.isNull(pageSize))
            pageSize = 10;

        if (startTime != null)
            $scope.searchEntity.startTime = new Date(Date.parse(startTime.replace(/-/g, "/")));

        if (endTime != null)
            $scope.searchEntity.endTime = new Date(Date.parse(endTime.replace(/-/g, "/")));


        extractRecordService.findPage(pageNum, pageSize, $scope.searchEntity).success(function (response) {

            if (response.code = 200) {

                //获取企业列表
                $scope.extractRecordList = response.data;

                //更新总记录数
                $scope.paginationConf.totalItems = response.total;

            } else {
                alert(response.msg);
            }

        });
    };


    //重置条件查询记录
    $scope.resetSearchEntity = function () {

        $scope.searchEntity = {};

        $scope.reloadList();
    };

    //监控 调度订单状态 orderStatus 变量
    $scope.$watch("searchEntity.orderStatus", function (newValue, oldValue) {

        if (newValue == null) return;

        $scope.reloadList();

    });

    //更新调度记录
    $scope.update = function () {

        console.log($scope.isClick);
        if ($scope.isClick) {
            return;
        }
        //标记为点击状态
        $scope.isClick = true;

        extractRecordService.update($scope.entity).success(function (response) {

            if (response != 200) {
                alert(response.msg);
            }

            //重新加载列表
            $scope.reloadList();

            //还原按钮初始状态
            $scope.isClick = false;

        }).error(function () {
            //还原按钮初始状态
            $scope.isClick = false;
        });

    };

    //初始化更新参数 - 驳回
    $scope.initEntity = function (id, orderStatus) {

        $scope.entity = {};

        $scope.entity.id = id;


        $scope.entity.orderStatus = orderStatus;

    };

    //初始化登录手机号 - 打款信息
    $scope.initManagerPhone = function (orderNo) {

        $scope.entity = {};

        var enCodeManagerStr = sessionStorage.getItem("MANAGER");

        // console.log(enCodeManagerStr);

        //base64解码
        var managerStr = decodeURIComponent(window.atob(enCodeManagerStr));

        var manager = JSON.parse(managerStr);

        $scope.entity.phone = manager.phone;

        if (!$scope.isNull(orderNo)) {
            $scope.entity.orderNo = orderNo;
        }
        ;

    };


    $scope.intervalTime = 60;
    //发送验证码
    $scope.isSendSmsSuccess = false;
    $scope.sendSms = function () {

        console.log($scope.isClick);
        if ($scope.isClick) {
            return;
        }
        //标记为点击状态
        $scope.isClick = true;

        if ($scope.isNull($scope.entity.phone)) {

            alert('手机号缺失');

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }

        smsService.sendCheck($scope.entity.phone).success(function (response) {

            if (response.code != 200) {
                alert(response.msg);
            } else {

                //发送成功
                $scope.isSendSmsSuccess = true;

            }
            //还原按钮初始状态
            $scope.isClick = false;
        }).error(function () {
            //还原按钮初始状态
            $scope.isClick = false;
        });


    };

    $scope.isScheduleSuccess = false;
    //调度出款
    $scope.schedule = function () {

        if ($scope.isClick) {
            console.log("重复提交");
            return;
        }
        //标记为点击状态
        $scope.isClick = true;


        if ($scope.isNull($scope.entity.phone)) {

            alert("手机号缺失");
            //还原按钮初始状态
            $scope.isClick = false;
            return;
        }

        if ($scope.isNull($scope.entity.code)) {

            alert("验证码缺失");
            //还原按钮初始状态
            $scope.isClick = false;
            return;
        }

        if ($scope.isNull($scope.entity.orderNo)) {

            alert("订单号缺失");

            //还原按钮初始状态
            $scope.isClick = false;

            return;
        }
        extractRecordService.schedule($scope.entity).success(function (response) {

            if (response.code == 200) {

                $scope.isScheduleSuccess = true;

                alert("银行受理成功");

                $scope.entity.code = null;

                $scope.reloadList();

            } else {

                alert(response.msg);
            }


            //还原按钮初始状态
            $scope.isClick = false;
        }).error(function () {
            //还原按钮初始状态
            $scope.isClick = false;
        });

    };

    //字符串格式化
    $scope.strParse = function (str) {

        if (str != null && str.indexOf("：") != -1) {
            str = str.substring(str.indexOf("：") + 1, str.length);
        }

        if (str != null && str.indexOf("(") != -1) {
            str = str.split("(")[0];
        }

        if (str != null && str.indexOf("[") != -1) {
            str = str.split("[")[0];
        }

        return str;
    };

    function interval() {
        var e = window.setInterval(function () {

            $scope.intervalTime = $scope.intervalTime - 1;

            if ($scope.intervalTime == 0) {

                window.clearInterval(e);
                $scope.isSendSmsSuccess = false;

            }

        }, 1000)
    };

    //获取未关闭税地的品牌服务费资金池
    $scope.findAllBrandTaxInfo = function () {
        // 1 未关闭
        brandTaxInfoService.selectByTaxIsClose(1).success(function (response) {
            if (response.code == 200) {
                $scope.brandTaxInfoList = response.data;
            }
        })
    };

    //监控资金调度类型
    $scope.$watch("entity.schedulingType", function (newValue, oldValue) {
        if (3 == newValue) {
            $scope.findAllTaxInfo();
            $scope.findAllBrandTaxInfo();
        } else {
            //清空旧值
            $scope.entity = {schedulingType: newValue};
        }
    })

    //查询服务费资金余额
    let findServiceChargeBalance = function (subAccount) {
        extractRecordService.findServiceChargeBalance(subAccount).success(function (res) {
            if (res.code == 200) {
                $scope.serviceChargeBalance = res.data;
            } else {
                $scope.serviceChargeBalance = res.mag;
            }
        })
    }

    //监控付款账户
    $scope.$watch("entity.payerAccount", function (newValue, oldValue) {
        //调度类型为服务费资金池
        if ($scope.entity.schedulingType == 3) {
            findServiceChargeBalance(newValue);
        }
    })

    //获取所有税地明细
    $scope.findAllTaxInfo = function () {

        taxInfoService.findAll().success(function (response) {

            if (response.code == 200){
                $scope.taxInfoList = response.data;
            }

        });
    };

    $scope.isDisplayPayerAccountName = false;
    $scope.brandTaxInfoList_display = [];
    $scope.$watch("entity.tax_id",function (newValue,oldValue) {
        $scope.brandTaxInfoList_display = [];
        $scope.entity.payerAccount = null;
        $scope.serviceChargeBalance = null;
        $scope.entity.payerAccountName = null;

        if ($scope.isNull(newValue)) {
            return;
        }
        $scope.isDisplayPayerAccountName = true;
        let btiList = $scope.brandTaxInfoList;
        let index = 0;
        for (let i = 0; i < btiList.length; i++) {

            if (btiList[i].taxInfoId == newValue){
                $scope.brandTaxInfoList_display[index] = btiList[i];
                index = index + 1;
            }
        }
    })
});